package com.xjx.service.impl;

import com.xjx.dao.UserDao;
import com.xjx.pojo.PageInfo;
import com.xjx.pojo.User;
import com.xjx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }

    @Override
    public User login(String userCode, String userPassword) {
        return userDao.login(userCode,userPassword);
    }

    @Override
    public PageInfo<User> findByPage(int currentPage,int size,String userName, String userCode, String startDate, String endDate) {
        PageInfo<User> pageInfo = new PageInfo<>();
        pageInfo.setSize(size);
        //获取总数据量
        int totalCount = userDao.count(userName,userCode,startDate,endDate);//中断
        pageInfo.setTotalCount(totalCount);
        int totalPage =  (int)Math.ceil(totalCount/(double)pageInfo.getSize());
        pageInfo.setTotalPage(totalPage);
        if(currentPage<1){
            pageInfo.setCurrentPage(1);
        }else if(currentPage > totalPage){
            pageInfo.setCurrentPage(totalPage);
        }else {
            pageInfo.setCurrentPage(currentPage);
        }
        int start = (pageInfo.getCurrentPage()-1)*pageInfo.getSize();
        if (start<0){
            start=0;
        }
        List<User> list = userDao.findByPage(start, pageInfo.getSize(),userName, userCode,startDate,endDate);
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public int save(User user) {
        return userDao.save(user);
    }

    @Override
    public User findById(Integer id) {
        return userDao.findById(id);
    }

    @Override
    public int modify(User user) {
        return userDao.modify(user);
    }

    @Override
    public int delete(Integer id) {
        return userDao.delete(id);
    }


}
